<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    Gulp 初识 |  虎虎生辉
  </title>
  
  <link rel="shortcut icon" href="/blog/favicon.ico" />
  
  
<link rel="stylesheet" href="/blog/css/style.css">

  
<script src="/blog/js/pace.min.js"></script>


  

  

<meta name="generator" content="Hexo 6.0.0"></head>

</html>

<body>
  <div id="app">
    <main class="content">
      <section class="outer">
  <article id="post-GULPa" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  Gulp 初识
</h1>
  

    </header>
    

    
    <div class="article-meta">
      <a href="/blog/2018/07/21/GULPa/" class="article-date">
  <time datetime="2018-07-20T16:00:00.000Z" itemprop="datePublished">2018-07-21</time>
</a>
      
    </div>
    

    
    
    <div class="tocbot"></div>





    

    <div class="article-entry" itemprop="articleBody">
      


      

      
      <h1 id="gulp概述"><a href="#gulp概述" class="headerlink" title="gulp概述"></a>gulp概述</h1><h2 id="（一）环境配置"><a href="#（一）环境配置" class="headerlink" title="（一）环境配置"></a>（一）环境配置</h2><ol>
<li>npm i -g gulp</li>
<li>npm init -y</li>
<li>npm i -D gulp</li>
<li>create gulpfile.js</li>
</ol>
<h2 id="（二）gulpfile-js"><a href="#（二）gulpfile-js" class="headerlink" title="（二）gulpfile.js"></a>（二）gulpfile.js</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> gulp = <span class="built_in">require</span>(<span class="string">&quot;gulp&quot;</span>); <span class="comment">//gulp4.0</span></span><br><span class="line"><span class="keyword">const</span> minifyHtml = <span class="built_in">require</span>(<span class="string">&quot;gulp-minify-html&quot;</span>); <span class="comment">//压缩html</span></span><br><span class="line"><span class="keyword">const</span> imagemin = <span class="built_in">require</span>(<span class="string">&#x27;gulp-imagemin&#x27;</span>);<span class="comment">//图片压缩</span></span><br><span class="line"><span class="keyword">const</span> minifyCss = <span class="built_in">require</span>(<span class="string">&quot;gulp-minify-css&quot;</span>); <span class="comment">//压缩CSS</span></span><br><span class="line"><span class="keyword">const</span> less = <span class="built_in">require</span>(<span class="string">&#x27;gulp-less&#x27;</span>); <span class="comment">//编译less</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> postcss = <span class="built_in">require</span>(<span class="string">&quot;gulp-postcss&quot;</span>);<span class="comment">//自动添加css3前缀</span></span><br><span class="line"><span class="keyword">const</span> autoprefixer = <span class="built_in">require</span>(<span class="string">&quot;autoprefixer&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> uglify = <span class="built_in">require</span>(<span class="string">&#x27;gulp-uglify&#x27;</span>); <span class="comment">//js压缩</span></span><br><span class="line"><span class="keyword">const</span> debug = <span class="built_in">require</span>(<span class="string">&quot;gulp-strip-debug&quot;</span>);<span class="comment">//清除js调试语句</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> connect = <span class="built_in">require</span>(<span class="string">&quot;gulp-connect&quot;</span>);<span class="comment">//服务器依赖</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//出入口路径</span></span><br><span class="line"><span class="keyword">const</span> folder = &#123;</span><br><span class="line">  <span class="attr">src</span>: <span class="string">&quot;src/&quot;</span>,</span><br><span class="line">  <span class="attr">dist</span>: <span class="string">&quot;dist/&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//任务队列</span></span><br><span class="line"><span class="keyword">const</span> taskArr = [];</span><br><span class="line"></span><br><span class="line"><span class="comment">//html任务</span></span><br><span class="line">gulp.task(<span class="string">&quot;html&quot;</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  gulp.src(folder.src + <span class="string">&quot;html/*&quot;</span>)</span><br><span class="line">      .pipe(connect.reload())</span><br><span class="line">      .pipe(minifyHtml())</span><br><span class="line">      .pipe(gulp.dest(folder.dist + <span class="string">&quot;html/&quot;</span>))</span><br><span class="line">&#125;)</span><br><span class="line">taskArr.push(<span class="string">&quot;html&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//css任务</span></span><br><span class="line">gulp.task(<span class="string">&quot;css&quot;</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  gulp.src(folder.src + <span class="string">&quot;css/*&quot;</span>)</span><br><span class="line">      .pipe(connect.reload())</span><br><span class="line">      .pipe(less())</span><br><span class="line">      .pipe(postcss([autoprefixer()]))</span><br><span class="line">      .pipe(minifyCss())</span><br><span class="line">      .pipe(gulp.dest(folder.dist + <span class="string">&quot;css/&quot;</span>))</span><br><span class="line">&#125;)</span><br><span class="line">taskArr.push(<span class="string">&quot;css&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//image任务</span></span><br><span class="line">gulp.task(<span class="string">&quot;image&quot;</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  gulp.src(folder.src + <span class="string">&quot;image/*&quot;</span>)</span><br><span class="line">      .pipe(imagemin())</span><br><span class="line">      .pipe(gulp.dest(folder.dist + <span class="string">&quot;image/&quot;</span>))</span><br><span class="line">&#125;)</span><br><span class="line">taskArr.push(<span class="string">&quot;image&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//js任务</span></span><br><span class="line">gulp.task(<span class="string">&quot;js&quot;</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  gulp.src(folder.src + <span class="string">&quot;js/*&quot;</span>)</span><br><span class="line">      .pipe(connect.reload())</span><br><span class="line">      .pipe(debug())</span><br><span class="line">      .pipe(uglify())</span><br><span class="line">      .pipe(gulp.dest(folder.dist + <span class="string">&quot;js/&quot;</span>))</span><br><span class="line">&#125;)</span><br><span class="line">taskArr.push(<span class="string">&quot;js&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//服务器任务</span></span><br><span class="line">gulp.task(<span class="string">&quot;server&quot;</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  connect.server(&#123;</span><br><span class="line">    <span class="attr">port</span>: <span class="number">9000</span>,<span class="comment">//端口</span></span><br><span class="line">    <span class="attr">livereload</span>:<span class="literal">true</span><span class="comment">//刷新</span></span><br><span class="line">  &#125;);</span><br><span class="line">&#125;)</span><br><span class="line">taskArr.push(<span class="string">&quot;server&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//监听任务</span></span><br><span class="line">gulp.task(<span class="string">&quot;watch&quot;</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  gulp.watch(folder.src + <span class="string">&quot;html/*&quot;</span>, gulp.series(<span class="string">&quot;html&quot;</span>));</span><br><span class="line">  gulp.watch(folder.src + <span class="string">&quot;css/*&quot;</span>, gulp.series(<span class="string">&quot;css&quot;</span>));</span><br><span class="line">  gulp.watch(folder.src + <span class="string">&quot;js/*&quot;</span>, gulp.series(<span class="string">&quot;js&quot;</span>));</span><br><span class="line">&#125;)</span><br><span class="line">taskArr.push(<span class="string">&quot;watch&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//默认执行任务队列</span></span><br><span class="line">gulp.task(<span class="string">&quot;default&quot;</span>, gulp.parallel(taskArr)) </span><br></pre></td></tr></table></figure>

      
      <!-- 打赏 -->
      
        <div id="reward-btn">
          打赏
        </div>
        
    </div>
    <footer class="article-footer">
      <a data-url="https://gitee.com/huafu123/blog/2018/07/21/GULPa/" data-id="ckya9fc10000dygf6c1pu03vl"
        class="article-share-link">分享</a>
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog/tags/gulp/" rel="tag">gulp</a></li></ul>

    </footer>

  </div>

  
  
  <nav class="article-nav">
    
      <a href="/blog/2018/08/01/VUEa/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            VUE 初识
          
        </div>
      </a>
    
    
      <a href="/blog/2018/07/11/WEBPACKa/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">Webpack 初识</div>
      </a>
    
  </nav>


  

  
  
<!-- valine评论 -->
<div id="vcomments-box">
    <div id="vcomments">
    </div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='https://cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>
<script>
    new Valine({
        el: '#vcomments',
        notify: false,
        verify: false,
        app_id: '',
        app_key: '',
        path: window.location.pathname,
        avatar: 'mp',
        placeholder: '给我的文章加点评论吧~',
        recordIP: true
    });
    const infoEle = document.querySelector('#vcomments .info');
    if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
        infoEle.childNodes.forEach(function (item) {
            item.parentNode.removeChild(item);
        });
    }
</script>
<style>
    #vcomments-box {
        padding: 5px 30px;
    }

    @media screen and (max-width: 800px) {
        #vcomments-box {
            padding: 5px 0px;
        }
    }

    #vcomments-box #vcomments {
        background-color: #fff;
    }

    .v .vlist .vcard .vh {
        padding-right: 20px;
    }

    .v .vlist .vcard {
        padding-left: 10px;
    }
</style>

  

  
  
  

</article>
</section>
      <footer class="footer">
  <div class="outer">
    <ul class="list-inline">
      <li>
        &copy;
        2020-2022
        Huafu Li
      </li>
      <li>
        
          Powered by
        
        
        <a href="https://hexo.io" target="_blank">Hexo</a> Theme <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul class="list-inline">
      <li>
        
        
        <ul class="list-inline">
  <li>PV:<span id="busuanzi_value_page_pv"></span></li>
  <li>UV:<span id="busuanzi_value_site_uv"></span></li>
</ul>
        
      </li>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>
    <div class="to_top">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>
      </div>
    </main>
    
    <aside class="sidebar">
      
        <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/blog/"><img src="/blog/hu.png" alt="虎虎生辉"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/blog/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/blog/archives">目录</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/blog/about">关于</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/blog/pictures">相册</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/blog/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
      </aside>
      <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/blog/./images/alipay.png">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/blog/./images/wechat.png">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
      
<script src="/blog/js/jquery-2.0.3.min.js"></script>


<script src="/blog/js/jquery.justifiedGallery.min.js"></script>


<script src="/blog/js/lazyload.min.js"></script>


<script src="/blog/js/busuanzi-2.3.pure.min.js"></script>


  
<script src="/blog/fancybox/jquery.fancybox.min.js"></script>




  
<script src="/blog/js/tocbot.min.js"></script>

  <script>
    // Tocbot_v4.7.0  http://tscanlin.github.io/tocbot/
    tocbot.init({
      tocSelector: '.tocbot',
      contentSelector: '.article-entry',
      headingSelector: 'h1, h2, h3, h4, h5, h6',
      hasInnerContainers: true,
      scrollSmooth: true,
      positionFixedSelector: '.tocbot',
      positionFixedClass: 'is-position-fixed',
      fixedSidebarOffset: 'auto',
			onClick: (e) => {
      	document.getElementById(e.target.innerText).scrollIntoView()
      	return false;
    	}
    });
  </script>


<script>
  var ayerConfig = {
    mathjax: false
  }
</script>


<script src="/blog/js/ayer.js"></script>


<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">



<script type="text/javascript" src="https://js.users.51.la/20544303.js"></script>
  
  

  </div>
</body>

</html>